/*
 *  Copyright 2010 mazhao.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package shopping.customer.onthefly;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;
import shopping.commons.Util;
import shopping.model.Order;

/**
 * Created by IntelliJ IDEA.
 * User: mazhao
 * Date: 2010/11/12
 * Time: 22:13:40
 * To change this template use File | Settings | File Templates.
 */
@org.apache.struts2.convention.annotation.Namespace("/customer/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value = "shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
        @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
        @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class OrderAction extends BaseAction {
    // =======================================
    // properties for communication
    // =======================================
    private int orderId;
    private int formerStatus;
    private int status;

    public int getFormerStatus() {
        return formerStatus;
    }

    public void setFormerStatus(int formerStatus) {
        this.formerStatus = formerStatus;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    // =======================================
    // action method
    // =======================================
     @Action(value = "/customer/onthefly/changestatus",
            results = {
                    @Result(name = "success", type = "json")
            })
    public String changeStatus() {

        log.debug("enter customer/onthefly/changestatus action");
        log.debug("change status for " + orderId + " from " + formerStatus + " to " + status);

        Order odr = new Order();
        odr.setId(orderId);
        odr.load();

        if(odr.getStatus() != formerStatus) {
            log.error("Can not update status, please check former status!");
        } else {
            String cn = Util.Security.retriveUserFromSession().getUsername();
            if(formerStatus == Order.ORDER_STATUS_NEW && status == Order.ORDER_STATUS_CANCELED){
                odr.newToCanceled(cn);
            } else if(formerStatus == Order.ORDER_STATUS_SENT && status == Order.ORDER_STATUS_FINISHED){
                odr.sentToFinished(cn);
            } else if(formerStatus == Order.ORDER_STATUS_PENDING && status == Order.ORDER_STATUS_CANCELED) {
                odr.pendingToCanceled(cn);
            } else {
                log.error("Can not update status, please check from status and to status!");
            }
        }

        return SUCCESS;
    }
}
